import 'package:equatable/equatable.dart';
import 'package:im_app/im/service/ImGroupService.dart';
import 'package:im_app/im/util/DbUtil.dart';
import 'package:im_app/im/util/ImSingleton.dart';

import '../../dao/ImGroupDao.dart';
import '../../dao/ImSessionDao.dart';
import '../../pojo/Enums.dart';
import '../../pojo/ImPojo.dart';
import '../../util/UiUtil.dart';
import 'BaseBloc.dart';

final class GroupSelectCubit extends BaseBloc<GroupSelectEvent, GroupSelectState> {
  GroupSelectCubit() : super(GroupSelectState.loading()) {
    on<GroupSelectShowEvent>((event, emit){
      _showGroup(this.state, event.group);
    });
  }

  Future<void> loadData() async {
    print('GroupSelectCubit loadData!!!!!!!!!!');
    var groupDao = ImGroupDao();

    var userId = ImSingleton.getInstance().session.userId!;
    List<ImGroup> groups = await groupDao.getByUserAndSize(userId, 10000);
    emit(GroupSelectState.success(groups));
  }

  void _showGroup(GroupSelectState state, ImGroup group) async {
    var sessionDao = ImSessionDao();

    var userId = ImSingleton.getInstance().session.userId!;
    var session = await sessionDao.getByGroupCodeAndUserId(group.code, userId);
    if (session != null) {
      UiUtil.uiShowSession(session);
      GroupSelectState newState = state.copy(imSession: session, forwardRouter: "/MessageSendPage");
      emit(newState);
    } else {
      session = ImSession.buildWithGroup(userId, group);
      var groupService = ImGroupService();
      await groupService.createImGroup(session);
      await groupService.rpcCreateGroup(session);
      UiUtil.uiShowSession(session);
    }
  }
}

final class GroupSelectState extends Equatable {
  final LoadingState state;
  final List<ImGroup> allGroups;
  final String errorMsg;
  final bool isError;
  final String forwardRouter;
  ImSession? imSession;

  GroupSelectState._({
    this.state = LoadingState.loading,
    this.allGroups = const <ImGroup>[],
    this.errorMsg = '',
    this.isError = false,
    this.forwardRouter = '',
    this.imSession = null,
  });

  GroupSelectState copy({List<ImGroup>? allGroups, bool? isError, String? errorMsg,
    String? forwardRouter, ImSession? imSession}) {
    return GroupSelectState._(
      allGroups: allGroups?? this.allGroups,
      isError: isError ?? this.isError,
      errorMsg: errorMsg?? this.errorMsg,
      state: this.state,
      forwardRouter: forwardRouter ?? this.forwardRouter,
      imSession: imSession?? this.imSession,
    );
  }

  GroupSelectState.loading() : this._();

  GroupSelectState.success(List<ImGroup> allGroups)
      : this._(state: LoadingState.success, allGroups: allGroups);

  GroupSelectState.forward(String forwardRouter, ImSession session) :
        this._(forwardRouter: forwardRouter, imSession: session);

  GroupSelectState.failure(String msg) : this._(state: LoadingState.failure, errorMsg: msg, isError: true);

  @override
  List<Object?> get props => [allGroups, state, isError, imSession, forwardRouter];

}

class GroupSelectEvent {}
class GroupSelectShowEvent extends GroupSelectEvent{
  final ImGroup group;

  GroupSelectShowEvent(this.group);
}